package com.frxuexi.user.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.CardColors
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseApp
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.user.R
import com.tencent.mmkv.MMKV
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Composable
fun MinePage(navHostController: NavHostController) {
    val kv = MMKV.defaultMMKV()

    var name by remember {
        mutableStateOf(kv.decodeString("name", "未登录"))
    }
    var role by remember {
        mutableStateOf(kv.decodeInt("role", 0))
    }

    var islogout by remember {
        mutableStateOf(false)
    }

    Column(
        Modifier
            .padding(20.dp)
            ) {
        Text(text = "我的", fontWeight = FontWeight.Bold, fontSize = 22.sp)
        Spacer(modifier = Modifier.height(10.dp))
        ElevatedCard(
            modifier = Modifier.fillMaxWidth(),
            colors = CardDefaults.elevatedCardColors(containerColor = Color.White)
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
                    .clickable {
                        if (name == "未登录") {
                            navHostController.navigate(RouterPath.LOGIN_PAGE)
                        } else {
                            islogout = true
                        }
                    }
            ) {
                AsyncImage(
                    model = "https://img1.baidu.com/it/u=3406115504,1068597125&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1725037200&t=3c5e09c88f402ee3f67ff412d5dae000",
                    contentDescription = "",
                    modifier = Modifier
                        .size(80.dp)
                        .clip(CircleShape),
                    contentScale = ContentScale.Crop
                )
                Spacer(modifier = Modifier.width(10.dp))
                Column(
                    modifier = Modifier.height(80.dp),
                    verticalArrangement = Arrangement.SpaceAround
                ) {
                    Text(text = name!!)
                    Text(text = if (role == 1) "经理" else if (role == 2) "产品经理" else if (role == 3) "员工" else "")
                }
            }
        }
        Spacer(modifier = Modifier.height(20.dp))
        Text(text = "常用功能")
        Spacer(modifier = Modifier.height(10.dp))
        ElevatedCard(
            modifier = Modifier.fillMaxWidth(),
            colors = CardDefaults.elevatedCardColors(containerColor = Color.White)
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(10.dp)
            ) {
                Column(Modifier.clickable {
                    navHostController.navigate(RouterPath.ASKLEAVELIST_PAGE)
                }) {
                    Image(painter = painterResource(id = R.drawable.qj), contentDescription = "",
                        Modifier.size(40.dp))
                    Spacer(modifier = Modifier.height(5.dp))
                    Text(text = "请假")
                }
                Spacer(modifier = Modifier.width(10.dp))
                if(role==1){
                    Column(Modifier.clickable {
                        navHostController.navigate(RouterPath.APPROVELIST_PAGE)
                    }) {
                        Image(painter = painterResource(id = R.drawable.sp), contentDescription = "",
                            Modifier.size(40.dp))
                        Spacer(modifier = Modifier.height(5.dp))
                        Text(text = "审批")
                    }
                }
            }
        }


    }

    if(islogout){
        AlertDialog(onDismissRequest = { islogout=false },
            confirmButton = { TextButton(onClick = {
                kv.remove("uid")
                kv.remove("name")
                kv.remove("role")
                name="未登录"
                role=0
                islogout=false
            }) {
                Text(text = "确认")
            } },
            dismissButton={ TextButton(onClick = { islogout=false }) {
                Text(text = "取消")
            }},
            title = { Text(text = "退出提醒")},
            text = { Text(text = "确认要退出吗？")})
    }

}